/* eslint-disable */
var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
var DEFAULT_PATTERN = "yyyy-MM-dd";
function padding(s, len) {
    var len = len - (s + "").length;
    for (var i = 0; i < len; i++) {
        s = "0" + s;
    }
    return s;
}

//时间 yyyy-mm-dd
export function getLocalDate(rtime) {
    var u1, t, s;
    u1 = rtime.split("T");
    t = u1[1].split(".");
    s = t[0].split(":");
    return u1[0];
}
//时间 yyyy-mm-dd hh:mm
export function getLocalTime(rtime) {
    var u1, t, s;
    u1 = rtime.split("T");
    t = u1[1].split(".");
    s = t[0].split(":");
    return u1[0] + " " + s[0] + ":" + s[1];
}
export function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var strMin = date.getMinutes();
    var strSec = date.getSeconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (strMin >= 0 && strMin <= 9) {
        strMin = "0" + strMin;
    }
    if (strSec >= 0 && strSec <= 9) {
        strSec = "0" + strSec;
    }
    var currentdate =
        date.getFullYear() +
        seperator1 +
        month +
        seperator1 +
        strDate +
        " " +
        date.getHours() +
        seperator2 +
        strMin +
        seperator2 +
        strSec;
    return currentdate;
}

//时间范围判断
export function checkDate(startTime, endTime) {
    if (startTime.length > 0 && endTime.length > 0) {
        var startTmp = startTime.split("-");
        var endTmp = endTime.split("-");
        var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
        var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
        if (sd.getTime() > ed.getTime()) {
            return false;
        }
    }
    return true;
}
//开始结束时间天数判断
export function daysBetween(sDate1, sDate2) {
    //Date.parse() 解析一个日期时间字符串，并返回1970/1/1 午夜距离该日期时间的毫秒数
    var time1 = Date.parse(new Date(sDate1));
    var time2 = Date.parse(new Date(sDate2));
    var nDays = Math.abs(parseInt((time2 - time1) / 1000 / 3600 / 24));
    return nDays;
}

function padLeftZero(str) {
    return ("00" + str).substr(str.length);
}

var formatDate = {
    format: function(date, pattern) {
        pattern = pattern || DEFAULT_PATTERN;
        return pattern.replace(SIGN_REGEXP, function($0) {
            switch ($0.charAt(0)) {
                case "y":
                    return padding(date.getFullYear(), $0.length);
                case "M":
                    return padding(date.getMonth() + 1, $0.length);
                case "d":
                    return padding(date.getDate(), $0.length);
                case "w":
                    return date.getDay() + 1;
                case "h":
                    return padding(date.getHours(), $0.length);
                case "m":
                    return padding(date.getMinutes(), $0.length);
                case "s":
                    return padding(date.getSeconds(), $0.length);
            }
        });
    },
    parse: function(dateString, pattern) {
        var matchs1 = pattern.match(SIGN_REGEXP);
        var matchs2 = dateString.match(/(\d)+/g);
        if (matchs1.length == matchs2.length) {
            var _date = new Date(1970, 0, 1);
            for (var i = 0; i < matchs1.length; i++) {
                var _int = parseInt(matchs2[i]);
                var sign = matchs1[i];
                switch (sign.charAt(0)) {
                    case "y":
                        _date.setFullYear(_int);
                        break;
                    case "M":
                        _date.setMonth(_int - 1);
                        break;
                    case "d":
                        _date.setDate(_int);
                        break;
                    case "h":
                        _date.setHours(_int);
                        break;
                    case "m":
                        _date.setMinutes(_int);
                        break;
                    case "s":
                        _date.setSeconds(_int);
                        break;
                }
            }
            return _date;
        }
        return null;
    }
};
export default formatDate;
